<?php
/**
 * usergroup.php
 * 
 * Created on 2014-10-22
 * Create by Calvin.Wang
 */

namespace haibao\cms\business;
use haibao\cms\web\common\Group;
class UserGroup extends \haibao\cms\business\Base{
	public static function getFuncCodesByGroupId($groupId){
		$data = self::getFuncCodesByGroupIds(array($groupId));
		
		return array_pop($data);
	}
	
	/**
	 * 根据grouIds获取functionCodes
	 * @param array $groupIds
	 */
	public static function getFuncCodesByGroupIds($groupIds){
		if (!$groupIds){
			throw new BusinessException('用户组ID不能为空',self::DATA_EMPTY_ID);
		}
		
		$data = new \haibao\cms\data\UserGroup();
		return $data->getFuncCodesByGroupIds($groupIds);
	}
	
	/**
	 * 
	 * @param \haibao\cms\model\data\UserGroupDefine $model
	 */
	public static function addUserGroup($model){
		self::checkOperation(self::FUNC_CODE_USERGROUP_ADD);
		
		if(!$model || (!$model instanceof \haibao\cms\model\data\UserGroupDefine)){
			throw new BusinessException("组不能为空",self::DATA_EMPTY);
		}
		if (!$model->GroupName){
			throw new BusinessException('用户组名称不能为空',self::DATA_EMPTY_NAME);
		}
		if (strlen($model->GroupName) > 200){
			throw new BusinessException('用户组名称过长',self::DATA_ERROR_NAME);
		}
		if(!$model->FuncCodes){
			throw new BusinessException("角色功能不能为空",self::DATA_EMPTY_FUNCTION_CODE);
		}
		
		if(self::hasSameGroupName(null,$model->GroupName)){
			throw new BusinessException('组名已存在',self::DATA_ERROR_NAME_EXIST);
		}
		
		if ($model->Status != \haibao\cms\model\data\UserGroupDefine::STATUS_DISABLE){
			$model->Status = \haibao\cms\model\data\UserGroupDefine::STATUS_ENABLE;
		}
		$model->CreateTime = new \DateTime();
		$model->CreateBy = self::getCurrentUser()->Id;
		
		$data = new \haibao\cms\data\UserGroup();
		$data->add($model);
		
		self::saveOperateHistory(self::FUNC_CODE_USERGROUP_ADD, $model->toArray(true,true));
	}
	
	public static function getGroupById($groupId){
		if(!$groupId){
			throw new BusinessException('用户组Id不能为空',self::DATA_EMPTY_ID);
		}
		$data = new \haibao\cms\data\UserGroup();
		$groupdata = $data->getOneById($groupId);
		if (!$groupdata){
			throw new BusinessException('用户组不存在',self::DATA_ERROR_NAME);
		}
		$groupdata->FuncCodes = self::getFuncCodesByGroupId($groupId);
		return $groupdata;
	}
	
	public static function hasSameGroupName($groupId,$groupName){
		if(!$groupName){
			throw new BusinessException('组名称不能为空',self::DATA_EMPTY_NAME);
		}
		$filter = new \haibao\cms\model\filter\UserGroupDefine();
		if($groupId){
			$filter->where(\haibao\cms\model\filter\UserGroupDefine::CONDITION_FIELD_NAME_ID,\haibao\frame\data\query\Condition::CONDITION_NOTEQUAL,$groupId);
		}
		
		$filter->where(\haibao\cms\model\filter\UserGroupDefine::CONDITION_FIELD_GROUP_NAME,\haibao\frame\data\query\Condition::CONDITION_EQUAL,$groupName);
		
		$data = new \haibao\cms\data\UserGroup();
		$models = $data->getAll($filter);
		$result = false;
		
		if ($models){
			$result = true;
		}
		
		return $result;
	}
	
	public static function getGroupsByFilter($userGroupDefineFilter){
		if (!$userGroupDefineFilter){
			$userGroupDefineFilter = new \haibao\cms\model\filter\UserGroupDefine(); 
		}
		else{
			//TODO check
		}
		$data = new \haibao\cms\data\UserGroup();
		return $data->getAll($userGroupDefineFilter);
	}
	
	public static function saveUsergroup($groupId,$arrSet){
		self::checkOperation(self::FUNC_CODE_USERGROUP_SAVE);
		
		if (!$groupId){
			throw new BusinessException('用户组ID不能为空',self::DATA_EMPTY_ID);
		}
		
		if(!$arrSet[\haibao\cms\model\filter\UserGroupDefine::CONDITION_FIELD_GROUP_NAME]){
			throw new BusinessException('组名不能为空',self::DATA_EMPTY_USER_GROUP);
		}
		if (strlen($arrSet[\haibao\cms\model\filter\UserGroupDefine::CONDITION_FIELD_GROUP_NAME]) > 200){
			throw new BusinessException('用户组名称过长',self::DATA_ERROR_NAME);
		}
		if(!$arrSet[\haibao\cms\model\filter\UserGroupDefine::CONDITION_FIELD_FUNCODES]){
			throw new BusinessException("角色功能不能为空",self::DATA_EMPTY_FUNCTION_CODE);
		}
		
		if(self::hasSameGroupName($groupId,$arrSet[\haibao\cms\model\filter\UserGroupDefine::CONDITION_FIELD_GROUP_NAME])){
			throw new BusinessException('组名已存在',self::DATA_ERROR_NAME_EXIST);
		}
		
		$data = new \haibao\cms\data\UserGroup();
		$data->saveGroup($groupId,$arrSet);
		
		$arrSet['Id'] = $groupId;
		self::saveOperateHistory(self::FUNC_CODE_USERGROUP_SAVE, $arrSet);
	}
	
	public static function deleteGroupById($groupId){
		self::checkOperation(self::FUNC_CODE_USERGROUP_DELTE);
		
		if(!$groupId){
			throw new BusinessException('用户组Id不能为空',self::DATA_EMPTY_ID);
		}
		$data = new \haibao\cms\data\UserGroup();
		$result = false;
		$arrSet = array();
		$userFilter = new \haibao\cms\model\filter\User();
		$userGroupDefineFilter = new \haibao\cms\model\filter\UserGroupDefine();
		$userGroupFilter = new \haibao\cms\model\filter\UserGroup();
		$userFilter->select(array(
			$userFilter::CONDITION_FIELD_NAME_ID,
			$userFilter::CONDITION_FIELD_NAME_USER_NAME
		));
		$userGroupFilter->select(array(
			$userGroupFilter::CONDITION_FIELD_NAME_GROUP_ID,
			$userGroupFilter::CONDITION_FIELD_NAME_USER_ID
		));
		$userGroupDefineFilter->select(array(
				$userGroupDefineFilter::CONDITION_FIELD_GROUP_NAME,
				$userGroupDefineFilter::CONDITION_FIELD_STATUS
		));
		$userFilter->leftJoin($userGroupFilter,null,$userFilter::CONDITION_FIELD_NAME_ID,$userGroupFilter::CONDITION_FIELD_NAME_USER_ID);
		$userFilter->leftJoin($userGroupDefineFilter,$userGroupFilter,$userGroupFilter::CONDITION_FIELD_NAME_GROUP_ID,$userGroupDefineFilter::CONDITION_FIELD_NAME_ID);
		$userFilter->where($userFilter::CONDITION_FIELD_NAME_ID, \haibao\frame\data\query\Condition::CONDITION_OR,array(
			new \haibao\frame\data\query\Condition($userFilter::CONDITION_FIELD_NAME_STATUS,\haibao\frame\data\query\Condition::CONDITION_EQUAL,\haibao\cms\model\data\User::STATUS_ENABLE) ,
			new \haibao\frame\data\query\Condition($userFilter::CONDITION_FIELD_NAME_STATUS,\haibao\frame\data\query\Condition::CONDITION_EQUAL,\haibao\cms\model\data\User::STATUS_FORCE_CHANGE_PASSWORD) 		
		));
		$userGroupDefineFilter->where($userGroupDefineFilter::CONDITION_FIELD_NAME_ID,\haibao\frame\data\query\Condition::CONDITION_EQUAL,$groupId);
		$userDate = new \haibao\cms\data\User();
		if($userDate->getAll($userFilter)){
			$result = true;
		}
		else{
			$data->updateById($groupId, array('Status'=>0));
			$userGroupPermfilter = new \haibao\cms\model\filter\UserGroupPermission();
			$userGroupPermfilter ->where($userGroupPermfilter::CONDITION_FIELD_GROUP_ID, \haibao\frame\data\query\Condition::CONDITION_EQUAL, $groupId);
			$userGroupPerm = new \haibao\cms\data\UserGroupPermission();
			$userGroupPerm->remove($userGroupPermfilter);
			$arrSet['Id'] = $groupId;
			
			self::saveOperateHistory(self::FUNC_CODE_USERGROUP_DELTE, $arrSet);
		}
		
		return $result;
	}
	
	public static function awakeGroupById($groupId){
		self::checkOperation(self::FUNC_CODE_USERGROUP_UNLOCK);
		
		if(!$groupId){
			throw new BusinessException('用户组Id不能为空',self::DATA_EMPTY_ID);
		}
		$data = new \haibao\cms\data\UserGroup();
		$data->updateById($groupId, array('Status'=>1));
		$arrSet = array();
		$arrSet['Id'] = $groupId;
		
		self::saveOperateHistory(self::FUNC_CODE_USERGROUP_DELTE,$arrSet);
		
	}
	/**
	 * 
	 * @param \haibao\cms\model\filter\User $filter
	 */
	public function getUserByFilter($filter){
		$data = new \haibao\cms\data\User();
		return $data->getAll($filter);
	}
	
	 public static function copyGroupById($groupId){
	 	self::checkOperation(self::FUNC_CODE_USERGROUP_ADD);
	 	
	 	$groupData = self::getGroupById($groupId);
	 	
	 	$model = new \haibao\cms\model\data\UserGroupDefine();
	 	$model->GroupName = $groupData->GroupName."[复制]" ;
	 	$model->Status = $groupData->Status;
	 	$model->FuncCodes = $groupData->FuncCodes;
	 	$model->CreateTime = new \DateTime();
	 	$model->CreateBy = self::getCurrentUser()->Id;
	 	
	 	if(self::hasSameGroupName(null, $model->GroupName)){
	 		$model->GroupName = $model->GroupName.mt_rand(0, 1000);
	 	}
	 	$data = new \haibao\cms\data\UserGroup();
	 	$data->add($model);
	 	
	 	self::saveOperateHistory(self::FUNC_CODE_USERGROUP_ADD, $model->toArray(true,true));
	 }
}
